package com.the4man.cnbdw.action;

import com.opensymphony.xwork2.ModelDriven;
import com.the4man.cnbdw.cache.DeviceCache;
import com.the4man.cnbdw.service.DeviceService;
import com.the4man.cnbdw.service.EmployeeDeviceService;
import com.the4man.cnbdw.service.LogInfoService;
import com.the4man.cnbdw.utils.Page;
import com.the4man.cnbdw.utils.SelectModel;
import com.the4man.framework.action.BaseAction;
import com.the4man.framework.model.*;
import com.the4man.framework.model.gen.DeviceExampleBase;
import com.the4man.framework.util.DateUtil;
import com.the4man.framework.util.LOG;
import com.the4man.framework.util.PageJson;
import com.the4man.framework.util.RequestUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

@Action(
        value = "deviceAction",
        results = {@Result(
                name = "addPage",
                location = "/cnbdw/device/add.jsp"
        ), @Result(
                name = "listPage",
                location = "/cnbdw/device/list.jsp"
        ), @Result(
                name = "editPage",
                location = "/cnbdw/device/edit.jsp"
        )}
)
public class DeviceAction extends BaseAction implements ModelDriven<Device> {
    private Device device = new Device();
    private LogInfo logInfo = new LogInfo();
    @Autowired
    private LogInfoService logService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private EmployeeDeviceService employeeDeviceService;
    @Autowired
    private DeviceCache deviceCache;

    public DeviceAction() {
    }

    public String indexPage() {
        return "listPage";
    }

    public String toAddPage() {
        String hardwareNo = this.getRequest().getParameter("cardSn");
        this.getRequest().setAttribute("hardwareNo", hardwareNo);
        return "addPage";
    }

    public String toEditPage() {
        try {
            Device d = (Device)this.deviceService.selectByPrimaryKey(this.getRequest().getParameter("id"), Device.class);
            BeanUtils.copyProperties(d, this.device);
        } catch (Exception var2) {
            var2.printStackTrace();
        }

        return "editPage";
    }

    public void getAllDatasourceList() {
        try {
            DeviceExample deviceExample = new DeviceExample();
            DeviceExampleBase.Criteria cc = deviceExample.createCriteria();
            if(StringUtils.isNotEmpty(this.device.getDeviceNo())) {
                cc.andDeviceNoLike("%" + this.device.getDeviceNo() + "%");
            }

            if(StringUtils.isNotEmpty(this.device.getHardwareNo())) {
                cc.andHardwareNoLike("%" + this.device.getHardwareNo() + "%");
            }

            if(StringUtils.isNotEmpty(this.device.getDeviceType())) {
                cc.andDeviceTypeEqualTo(this.device.getDeviceType());
            }

            if(StringUtils.isNotEmpty(this.device.getCardNum())) {
                cc.andCardNumLike("%" + this.device.getCardNum() + "%");
            }

            String stime = this.getRequest().getParameter("stime");
            String etime = this.getRequest().getParameter("etime");
            if(StringUtils.isNotEmpty(stime) && StringUtils.isEmpty(etime)) {
                cc.andRegisterTimeGreaterThanOrEqualTo(stime);
            }

            if(StringUtils.isEmpty(stime) && StringUtils.isNotEmpty(etime)) {
                cc.andRegisterTimeLessThanOrEqualTo(etime);
            }

            if(StringUtils.isNotEmpty(stime) && StringUtils.isNotEmpty(etime)) {
                cc.andRegisterTimeGreaterThanOrEqualTo(stime).andRegisterTimeLessThanOrEqualTo(etime);
            }

            int total = this.deviceService.countByExample(deviceExample);
            Page<Device> page = new Page();
            this.setPageProperties(page);
            this.deviceService.selectByExample(deviceExample, page);
            DataGrid d = new DataGrid();
            d.setRows(page.getData());
            d.setTotal(Long.valueOf((long)total));
            this.writeJson(d);
        } catch (Exception var8) {
            LOG.error("获取数据失败", var8, new String[0]);
        }

    }

    public void add() {
        PageJson json = new PageJson();

        try {
            DeviceExample deviceExample = new DeviceExample();
            deviceExample.createCriteria().andDeviceNoEqualTo(this.device.getDeviceNo());
            this.deviceService.selectByExample(deviceExample);
            DeviceExample deviceExample1 = new DeviceExample();
            deviceExample1.createCriteria().andCardNumEqualTo(this.device.getCardNum());
            List list1 = this.deviceService.selectByExample(deviceExample1);
            if(list1 != null && !list1.isEmpty()) {
                json.setReturnFlag(false);
                json.setReturnMsg("存在相同卡号的设备");
                this.writeJson(json);
                return;
            }

            String time = DateUtil.getInstance().formatDate(new Date());
            this.device.setId(UUID.randomUUID().toString());
            this.device.setCreateTime(time);
            this.device.setUpdateTime(time);
            this.device.setCreateUser(RequestUtil.getSessionUserinfo().getUsername());
            this.device.setUpdateUser(RequestUtil.getSessionUserinfo().getUsername());
            this.deviceService.insert(this.device);
            this.logInfo.setId(UUID.randomUUID().toString());
            this.logInfo.setTime(time);
            this.logInfo.setUserName(RequestUtil.getSessionUserinfo().getUsername());
            this.logInfo.setUserId(RequestUtil.getSessionUserinfo().getUserid());
            this.logInfo.setFunc("设备管理");
            this.logInfo.setOp("设备添加");
            this.logInfo.setMemo("设备名:" + this.device.getCardNum());
            this.logService.insert(this.logInfo);
            this.deviceCache.putMac(this.device);
            json.setReturnFlag(true);
            json.setReturnMsg("提交成功！");
        } catch (Exception var7) {
            json.setReturnFlag(false);
            json.setReturnMsg("提交出错了");
            LOG.error("获取数据失败", var7, new String[0]);
        }

        this.writeJson(json);
    }

    public void update() {
        PageJson json = new PageJson();

        try {
            DeviceExample deviceExample = new DeviceExample();
            deviceExample.createCriteria().andDeviceNoEqualTo(this.device.getDeviceNo()).andIdNotEqualTo(this.device.getId());
            List list = this.deviceService.selectByExample(deviceExample);
            if(list != null && !list.isEmpty()) {
                json.setReturnFlag(false);
                json.setReturnMsg("存在相同mac的设备");
                this.writeJson(json);
                return;
            }

            DeviceExample deviceExample1 = new DeviceExample();
            deviceExample1.createCriteria().andCardNumEqualTo(this.device.getCardNum()).andIdNotEqualTo(this.device.getId());
            List list1 = this.deviceService.selectByExample(deviceExample1);
            if(list1 != null && !list1.isEmpty()) {
                json.setReturnFlag(false);
                json.setReturnMsg("存在相同卡号的设备");
                this.writeJson(json);
                return;
            }

            Device d = (Device)this.deviceService.selectByPrimaryKey(this.device.getId(), Device.class);
            this.device.setUpdateTime(DateUtil.getInstance().formatDate(new Date()));
            this.device.setUpdateUser(RequestUtil.getSessionUserinfo().getUsername());
            this.deviceService.updateByPrimaryKey(this.device);
            this.logInfo.setId(UUID.randomUUID().toString());
            this.logInfo.setTime(DateUtil.getInstance().formatDate(new Date()));
            this.logInfo.setUserName(RequestUtil.getSessionUserinfo().getUsername());
            this.logInfo.setUserId(RequestUtil.getSessionUserinfo().getUserid());
            this.logInfo.setFunc("设备管理");
            this.logInfo.setOp("设备修改");
            this.logInfo.setMemo("设备名:" + this.device.getCardNum());
            this.logService.insert(this.logInfo);
            if(!StringUtils.equals(d.getDeviceNo(), this.device.getDeviceNo())) {
                this.deviceCache.putMac(this.device);
                this.deviceCache.removeMac(d.getDeviceNo());
            }

            json.setReturnFlag(true);
            json.setReturnMsg("提交成功！");
        } catch (Exception var7) {
            json.setReturnFlag(false);
            json.setReturnMsg("提交出错了");
            LOG.error("获取数据失败", var7, new String[0]);
        }

        this.writeJson(json);
    }

    private Device getCardNum() {
        return null;
    }

    public String executeModifyResult() {
        try {
            Object instance = this.deviceService.selectByPrimaryKey(this.device.getId(), Device.class);
            this.getRequest().setAttribute("instance", instance);
        } catch (Exception var2) {
            LOG.error("获取数据失败", var2, new String[0]);
        }

        return "modifyPage";
    }

    public void delete() {
        PageJson json = new PageJson();

        try {
            int count = 0;
            int bindCount = 0;
            String ids = this.getRequest().getParameter("ids");
            if(ids.contains(",")) {
                String[] str = ids.split(",");
                String[] var9 = str;
                int var8 = str.length;

                for(int var7 = 0; var7 < var8; ++var7) {
                    String info = var9[var7];
                    Device d = (Device)this.deviceService.selectByPrimaryKey(info, Device.class);
                    int i = this.deviceService.deleteByPrimaryKey(info, Device.class);
                    this.logInfo.setId(UUID.randomUUID().toString());
                    this.logInfo.setTime(DateUtil.getInstance().formatDate(new Date()));
                    this.logInfo.setUserName(RequestUtil.getSessionUserinfo().getUsername());
                    this.logInfo.setUserId(RequestUtil.getSessionUserinfo().getUserid());
                    this.logInfo.setFunc("设备管理");
                    this.logInfo.setOp("设备批量删除");
                    this.logInfo.setMemo("设备名:" + d.getCardNum());
                    this.logService.insert(this.logInfo);
                    if(i > 0) {
                        if(d != null) {
                            this.deviceCache.removeMac(d.getDeviceNo());
                        }

                        ++count;
                    }
                }
            } else {
                EmployeeDeviceExample example = new EmployeeDeviceExample();
                example.createCriteria().andDevIdEqualTo(ids);
                bindCount = this.employeeDeviceService.countByExample(example);
                if(bindCount <= 0) {
                    Device d = (Device)this.deviceService.selectByPrimaryKey(ids, Device.class);
                    count = this.deviceService.deleteByPrimaryKey(ids, Device.class);
                    this.logInfo.setId(UUID.randomUUID().toString());
                    this.logInfo.setTime(DateUtil.getInstance().formatDate(new Date()));
                    this.logInfo.setUserName(RequestUtil.getSessionUserinfo().getUsername());
                    this.logInfo.setUserId(RequestUtil.getSessionUserinfo().getUserid());
                    this.logInfo.setFunc("设备管理");
                    this.logInfo.setOp("设备删除");
                    this.logInfo.setMemo("设备名:" + d.getCardNum());
                    this.logService.insert(this.logInfo);
                    if(count > 0 && d != null) {
                        this.deviceCache.removeMac(d.getDeviceNo());
                    }
                }
            }

            if(bindCount > 0) {
                json.setReturnMsg("设备使用中，请先解绑设备！");
                json.setReturnFlag(false);
            } else if(count > 0) {
                json.setReturnMsg("成功删除" + count + "个设备！");
                json.setReturnFlag(true);
            } else {
                json.setReturnMsg("删除设备失败！");
                json.setReturnFlag(false);
            }
        } catch (Exception var12) {
            LOG.error("批量删除设备失败！", var12, new String[0]);
            json.setReturnMsg("删除设备失败！");
            json.setReturnFlag(false);
        }

        this.writeJson(json);
    }

    public void verify() {
        PageJson json = new PageJson();
        this.writeJson(json);
    }

    public void getDeviceTypeSelectData() {
        try {
            List<SelectModel> list = new ArrayList();
            list.add(new SelectModel("1", "手表"));
            list.add(new SelectModel("2", "手环1"));
            list.add(new SelectModel("3", "手环2"));
            list.add(new SelectModel("4", "智能卡"));
            list.add(new SelectModel("5", "人员卡"));
            this.writeJson(list);
        } catch (Exception var2) {
            LOG.error("获取数据失败", var2, new String[0]);
        }

    }

    public void getDeviceStateData() {
        try {
            List<SelectModel> list = new ArrayList();
            list.add(new SelectModel("1", "正常"));
            list.add(new SelectModel("2", "报废"));
            list.add(new SelectModel("3", "维修"));
            list.add(new SelectModel("4", "带离"));
            this.writeJson(list);
        } catch (Exception var2) {
            LOG.error("获取数据失败", var2, new String[0]);
        }

    }

    public Device getModel() {
        return this.device;
    }
}
